<?php

class InformationController extends Controller
{
	
	public function filters()
		{
			return array(
	              'accessControl',
			);
		}
	
		public function accessRules()
		{
			return array(
				array('allow',
	                'actions'=>array('Index','Read'),
	                'roles'=>array('admin'),
			),
			
			array('deny',
	                'actions'=>array(''),
	                'users'=>array('*'),
			),
			);
		}
	
	
	public function actionIndex()
	{
		
		  $page_size =4; 
         $page;
         
         $tag;
         $information_count = Information::model()->count();
          $count_page= $information_count/$page_size;
        
          $tags = InformationCategory::model()->findAll();
	if(!isset($_GET['tag']))
	{
         if(!isset($_GET['page']))
         {         	
         		$information=Information::model()->recently(0,$page_size)->findAll();                   
         }
         else
           {
           	$page=$_GET['page'];
             $sdvig = $page_size*($page-1);
             $information=Information::model()->recently( $sdvig,$page_size)->findAll();
           }
        
	}	
	else //���� ��� ���� �� ������� ��� ������� �� ����
	{
		$tag=$_GET['tag']; 
		$information_count = Information::model()->with("InformationCategory")->count('InformationCategory.text=:text',array(':text'=>$tag));
         $count_page=$information_count/$page_size;
	   if(!isset($_GET['page']))
         {        
        	
         		$information=Information::model()->with('InformationCategory')->recently(0,$page_size)->findAll('InformationCategory.text=:text',array(':text'=>$tag));
                   
         }
         else
           {
           
           	$page=$_GET['page'];
             $sdvig = $page_size*($page-1);
             $information=Information::model()->with('InformationCategory')->recently( $sdvig,$page_size)->findAll('InformationCategory.text=:text',array(':text'=>$tag));
           }
	}
    
      
   

  
    $this->render('index', array(       
        'information'=>$information,
        'count_page'=>$count_page+1,
    'tags'=>$tags,
        
    ));
		
		
		
			
			
	}
	
	public function actionRead() 
	{
		  $tags = InformationCategory::model()->findAll();
			$slider = Information::model()->findByPk($_GET['id']);
			
			  $this->render('read',array('information'=>$slider,'tags'=>$tags));
	}

	// Uncomment the following methods and override them if needed
	/*
	public function filters()
	{
		// return the filter configuration for this controller, e.g.:
		return array(
			'inlineFilterName',
			array(
				'class'=>'path.to.FilterClass',
				'propertyName'=>'propertyValue',
			),
		);
	}

	public function actions()
	{
		// return external action classes, e.g.:
		return array(
			'action1'=>'path.to.ActionClass',
			'action2'=>array(
				'class'=>'path.to.AnotherActionClass',
				'propertyName'=>'propertyValue',
			),
		);
	}
	*/
}